package cn.runagain.demo.wechatminiappdemo.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.client.RestTemplate;

/**
 * created by
 *
 * @author tuxianchao
 * @date 2018/7/2 下午6:49
 */
public class GetuserInfo {


    public static void main(String[] args) {
        String appId = "wxc8dd5fe4e06e69d2";
        String appSecret = "9596b881a90f57a6422b71d61979c68f";
        StringBuffer wechatUrl = new StringBuffer();

        String code = "0238YbLA0lcx3g2SXsLA0fcxLA08YbL-";
        //微信登录url
        wechatUrl.append("https://api.weixin.qq.com/sns/jscode2session")
                .append("?appid=" + appId)
                .append("&secret=" + appSecret)
                .append("&js_code=" + code)
                .append("&grant_type=authorization_code");
        //请求微信服务器登录
        RestTemplate restTemplate = new RestTemplate();
        String wechatLoginResult = restTemplate.getForObject(wechatUrl.toString(), String.class);

        JSONObject jsonResult = JSONUtil.parseObj(wechatLoginResult);
        //session_key && openid && unionid
        String sessionKey = (String) jsonResult.get("session_key");
        String openId = (String) jsonResult.get("openid");
        String unionid = (String) jsonResult.get("unionid");

        System.out.println(jsonResult.toString());
    }
}
